<?php defined('SYSPATH') or die('No direct script access.');
     
class Model_Images extends Model {
	protected $_table_name = 'images';
			

	public function check_extension($type) {
		if(isset($type) && is_string($type) && $type !== '') {
			$arr_type = array(
				'image/png' => '.png',
				'image/jpeg' => '.jpg',
			);
			if(isset($arr_type[$type])) {
				return $arr_type[$type];
			}
			else {
				return false;
			}
		}
		else {
			return false;
		}
	}

	public function add_images($image,$text){
		$img_folder = 'media/uploads/';
		if(count($image) > 0) {
			$img_obj = Image::factory($image['tmp_name']);
			$ext = $this->check_extension($img_obj->mime);
			if($ext !== false) {
				$file_name = md5(time().$image['name']).$ext;
				$file_name_full = 'full_'.$file_name;
				$file_name_mini = 'mini_'.$file_name;

				$x = $img_obj->save($img_folder.$file_name_full, 100);  
				$x = $img_obj->resize(150, 200)
					->save($img_folder.$file_name_mini, 100);
				
				$data = array(		
					'full'	=>	$file_name_full,
					'mini'	=>	$file_name_mini,
					'text'	=>	$text
				);
				$images = ORM::factory('image');
				$images->full = $file_name_full;
				$images->mini = $file_name_mini;
				$images->text = $text;
				$images->save();
			}
		}
		return true;
	}
	
}
     
            